package com.javajun.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.javajun.douban.DoubanUtils;
import com.javajun.douban.entity.BookJson;
import com.javajun.douban.entity.Result;
import com.javajun.entity.Book;
import com.javajun.service.BookService;

@RestController
public class DoubanTestController {
    
    @Autowired
    private BookService bookService;

    @RequestMapping(value="/book/getbook")
    private Object douban(String isbn) {
        ObjectMapper mapper = new ObjectMapper();
        DoubanUtils douban = new DoubanUtils();
        if (isbn == null) {
            isbn = "9787121341168";
        }
        Result result = douban.GetBookInfoByIsbn(isbn);
        BookJson json = new BookJson();
        try {
            json = mapper.readValue(result.getBookJson(), BookJson.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }
    
    @RequestMapping(value="/book/basebook")
    public Object baseBook(String isbn) {
        if(isbn == null) {
            isbn = "9787121341168";
        }
        Book book = bookService.findByIsbn(isbn);
        return book;
    }
}
